<template>
	<div class="wrapper">
		<!-- 搜索栏，如果在楼层装修顶部则会自动浮动，否则不浮动 -->
		<u-navbar class="navbar" :is-back="false" :is-fixed="true">
			<search style="width: 100%" />
			<!-- #ifndef H5 -->
			<!-- 扫码功能 不兼容h5 详情文档: https://uniapp.dcloud.io/api/system/barcode?id=scancode -->
			<!-- <div slot="right" class="navbar-right">
				<u-icon name="scan" @click="scan()" color="#666" size="50"></u-icon>
			</div> -->
			<!-- #endif -->
		</u-navbar>
		<!--banner-->
		<carousel :res="bannerList" />
		<menuLayout :res="categoryList" />
		<!-- uni 中不能使用 vue component 所以用if判断每个组件 -->
		<goods :enableBottomLoad="enableLoad" :res="goodsList" />

		<u-no-network @retry="init" @isConnected="isConnected"></u-no-network>
	</div>
</template>

<script>
	// 引用组件
	import tpl_banner from "@/pages/tabbar/home/template/tpl_banner"; //导航栏模块
	import tpl_menu from "@/pages/tabbar/home/template/tpl_menu"; //五列菜单模块
	import tpl_search from "@/pages/tabbar/home/template/tpl_search"; //搜索栏
	import tpl_goods from "@/pages/tabbar/home/template/tpl_goods"; //商品分类以及分类中的商品
	// 结束引用组件
	import permission from "@/js_sdk/wa-permission/permission.js"; //权限工具类
	import config from "@/config/config";
	import storage from "@/utils/storage.js";
	import {
		getBannerList
	} from "@/api/home.js"
	import {
		getCategoryList,
		getGoodsList
	} from "@/api/goods.js"
	export default {
		data() {
			return {
				config,
				storage,
				showCp: true,
				pageData: "", //楼层页面数据
				isIos: "",
				enableLoad: false, //触底加载 针对于商品模块
				bannerList: [],
				categoryList: [],
				goodsList: []
			};
		},
		components: {
			carousel: tpl_banner,
			menuLayout: tpl_menu,
			search: tpl_search,
			goods: tpl_goods
		},

		mounted() {
			this.init();
			this.getBannerList()
			this.getCategoryList()
			this.getGoodsList()
			// #ifdef MP-WEIXIN
			// 小程序默认分享
			uni.showShareMenu({
				withShareTicket: true
			});
			// #endif

		},
		methods: {
			getBannerList() {
				getBannerList().then(res => {
					this.bannerList = res.data.data
					// console.log(this.bannerList)
				})
			},
			getCategoryList() {
				getCategoryList({
					is_home: 1
				}).then(res => {
					this.categoryList = res.data.data.list
				})
			},
			getGoodsList() {
				getGoodsList({
					page: 1,
					limit: 20
				}).then(res => {
					this.goodsList = res.data.data.list
					console.log(this.goodsList)
				})
			},
			/**
			 * 实例化首页数据楼层
			 */
			init() {

			},
			// 是否有网络链接
			isConnected(val) {
				val ? this.init() : ''
			},

			/**
			 * TODO 扫码功能后续还会后续增加
			 * 应该实现的功能目前计划有：
			 * 扫描商品跳转商品页面
			 * 扫描活动跳转活动页面
			 * 扫描二维码登录
			 * 扫描其他站信息 弹出提示，返回首页。
			 */
			scanCode() {
				uni.scanCode({
					success: function(res) {
						let path = encodeURIComponent(res.result);



						if (path != undefined && path.indexOf("QR_CODE_LOGIN_SESSION") == 0) {
							console.log(path)
							//app扫码登录
							uni.navigateTo({
								url: "/pages/passport/scannerCodeLoginConfirm?token=" + path
							});
							return;
						}


						// WX_CODE 为小程序码
						if (res.scanType == "WX_CODE") {
							console.log(res);
							uni.navigateTo({
								url: `/${res.path}`,
							});
						} else {
							config.scanAuthNavigation.forEach((src) => {
								if (res.result.indexOf(src) != -1) {
									uni.navigateTo({
										url: `/${res.result.substring(src.length)}`,
									});
								} else {
									setTimeout(() => {
										uni.navigateTo({
											url: "/pages/tabbar/home/web-view?src=" + path,
										});
									}, 100);
								}
							});
						}
					},
				});
			},

			/**
			 * 提示获取权限
			 */
			tipsGetSettings() {
				uni.showModal({
					title: "提示",
					content: "您已经关闭相机权限,去设置",
					success: function(res) {
						if (res.confirm) {
							if (this.isIos) {
								plus.runtime.openURL("app-settings:");
							} else {
								permission.gotoAppPermissionSetting();
							}
						}
					},
				});
			},

			/**
			 * 唤醒客户端扫码
			 * 没权限去申请权限，有权限获取扫码功能
			 */
			async scan() {
				// #ifdef APP-PLUS
				this.isIos = plus.os.name == "iOS";
				// 判断是否是Ios
				if (this.isIos) {
					const iosFirstCamera = uni.getStorageSync("iosFirstCamera"); //是不是第一次开启相机
					if (iosFirstCamera !== "false") {
						uni.setStorageSync("iosFirstCamera", "false"); //设为false就代表不是第一次开启相机
						this.scanCode();
					} else {
						if (permission.judgeIosPermission("camera")) {
							this.scanCode();
						} else {
							// 没有权限提醒是否去申请权限
							this.tipsGetSettings();
						}
					}
				} else {
					/**
					 * TODO 安卓 权限已经授权了，调用api总是显示用户已永久拒绝申请。人傻了
					 * TODO 如果xdm有更好的办法请在 https://gitee.com/beijing_hongye_huicheng/gjRent/issues 提下谢谢
					 */
					this.scanCode();
				}

				// #endif

				// #ifdef MP-WEIXIN
				this.scanCode();
				// #endif
			},
		},
	};
</script>

<style scoped lang="scss">
	.navbar-right {
		padding: 0 16rpx 0 0;
	}
</style>